package com.lyw.mine.ui

import android.content.Intent
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import coil.load
import com.lu.library.ChoosePicManager
import com.lu.library.base.BaseActivity
import com.lyw.mine.R
import com.lyw.mine.databinding.ActivityEditUserInfoBinding
import com.lyw.mine.vm.EditUserInfoIntent
import com.lyw.mine.vm.EditUserInfoUiState
import com.lyw.mine.vm.EditUserInfoVm
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import me.shouheng.utils.ktx.onDebouncedClick


@AndroidEntryPoint
class EditUserInfoActivity : BaseActivity<ActivityEditUserInfoBinding>() {

    private val editUserInfoVm by viewModels<EditUserInfoVm>()
    private lateinit var choosePicManager:ChoosePicManager

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        choosePicManager.onActivityResult(requestCode,resultCode,data)
    }
    override fun initListener() {
        super.initListener()
        choosePicManager = ChoosePicManager(this){
            binding.ivHead.load(it)
            choosePicManager.dismiss()
            editUserInfoVm.sendUiIntent(EditUserInfoIntent.EditUserHead(it.toString()))
        }
        binding.etUserName.addTextChangedListener {
            it?.let {
                editUserInfoVm.sendUiIntent(EditUserInfoIntent.EditUserName(it.toString()))
            }

        }

        binding.tvSex.onDebouncedClick {
            editUserInfoVm.sendUiIntent(EditUserInfoIntent.ShowUserSexDialog(this))
        }
        binding.tvHeight.onDebouncedClick {
            editUserInfoVm.sendUiIntent(EditUserInfoIntent.ShowUserHeightDialog(this))
        }
        binding.tvWeight.onDebouncedClick {
            editUserInfoVm.sendUiIntent(EditUserInfoIntent.ShowUserWeightDialog(this))
        }
        binding.tvBirthday.onDebouncedClick {
            editUserInfoVm.sendUiIntent(EditUserInfoIntent.ShowUserBirthDayDialog(this))
        }
//        PathSelectorConfig.setDebug(true);
        binding.ivHead.onDebouncedClick {
            choosePicManager.showDialog()
//            PathSelector.build(this,MConstants.BUILD_DIALOG).setMorePopupItemListeners(object:CommonItemListener("OK"){
//                override fun onClick(
//                    v: View?,
//                    tv: TextView?,
//                    selectedFiles: MutableList<FileBean>?,
//                    currentPath: String?,
//                    pathSelectFragment: BasePathSelectFragment?
//                ): Boolean {
//                    pathSelectFragment?.dismiss()
//                    selectedFiles?.forEach {fileBean: FileBean ->
//                        LogUtils.d(fileBean)
//                    }
//                    return false;
//                }
//            }).show()
        }
    }

    override fun initData() {



        lifecycleScope.launch {

            editUserInfoVm.uiStateFlow.collect{

                when(it){
                    is EditUserInfoUiState.INIT->{
                        binding.etUserName.setText(it.userInfo.name)
                        binding.tvSex.setText(it.userInfo.sex)
                        binding.tvHeight.setText(it.userInfo.height)
                        binding.tvWeight.setText(it.userInfo.weight)
                        binding.tvBirthday.setText(it.userInfo.birthday)
                        it.userInfo.head?.let {head->
                            binding.ivHead.load(head)
                        }
                    }
                    else->{

                    }
                }
            }
        }

    }

    override fun layoutResId(): Int = R.layout.activity_edit_user_info
}